package com.cloudspace.jindun.activity.Woyao;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class PassActivitye extends CustomTitleBaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.cell_et_xgmm)
    EditText mCellEtXgmm;
    @Bind(R.id.call_bt)
    Button mCallBt;
    @Bind(R.id.rl_text_title)
    TextView mRlTextTitle;
    private String mPass = "1";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cell_activitye, false);
        ButterKnife.bind(this);
        setView();
    }

    private void setView() {
        mRlTextTitle.setText(getResources().getString(R.string.change_password));
        mCallBt.setText(getResources().getString(R.string.change_password_next_step));
        mCellEtXgmm.setHint(getResources().getString(R.string.change_password_old_password));
    }

    @OnClick({R.id.back, R.id.call_bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.call_bt:
                mPass = mCellEtXgmm.getText().toString();

                requestData(YXHttpUrl.WD_GPASSWORD, UserSessionManager.getInstance().getUserId(), mPass);
                break;
        }
    }

    public Dialog showDialog(final Context context, int type) {
        View view = LayoutInflater.from(context).inflate(R.layout.alert_dialogpass, null);

        final Dialog dialog = new AlertDialog.Builder(context).setView(view).create();

        dialog.show();
        TextView left = (TextView) view.findViewById(R.id.pass_text_left);
        TextView right = (TextView) view.findViewById(R.id.pass_text_right);

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        return dialog;
    }

    private void requestData(String url, String userId, String userPhone) {
        OkHttpUtils.post()
                .url(url)
                .addParams("userId", userId)
                .addParams("userPassword", userPhone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMaker.showToastShort(R.string.generic_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        CommonResult result = gson.fromJson(response, CommonResult.class);
                        if (result != null && result.status == 200) {
                            Intent intent = new Intent();
                            intent.setClass(PassActivitye.this, ResetPasswordActivitye.class);
                            startActivity(intent);
                            finish();
                        } else {
                            showDialog(PassActivitye.this, 1);
                        }
                    }
                });
    }
}
